package com.yft.data.service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sun.jmx.snmp.Timestamp;
import com.yft.data.Teacher;
import com.yft.data.dao.PublicDao;
import com.yft.data.util.MapToBean;

/**
 * @author xipeifeng
 * 
 */
@Service
public class InfoService {
	private Logger logger = Logger.getLogger(InfoService.class);
	@Autowired
	private PublicDao publicDao;
	
	DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
	
	public List<Map<String, Object>> getInfoTypeList(int pageno, int pagesize){
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT INFOTYPEID, INFONAME, INFODESC, CREATEDATE, UPDATEDATE, STATE ");
		sb.append("FROM INFOTYPE ");
		sb.append("LIMIT ");
		sb.append((pageno - 1) * pagesize);
		sb.append(", " + pagesize);
		String sql = sb.toString();
		logger.info(sql);
		return publicDao.getObjectList(sql);
	}

	public List<Map<String, Object>> getInfoListByType(int type, int pageno,
			int pagesize) {
		StringBuffer sb = new StringBuffer();
		switch (type) {
		case 1:
			sb.append("SELECT INFOTYPEID, INFONAME, INFODESC, CREATEDATE, UPDATEDATE, STATE ");
			sb.append("FROM INFOTYPE ");
			break;
		case 2:
			sb.append("SELECT INFOTYPEID, INFONAME, INFODESC, CREATEDATE, UPDATEDATE, STATE ");
			sb.append("FROM INFOTYPE ");
			break;
		case 3:
			sb.append("SELECT INFOTYPEID, INFONAME, INFODESC, CREATEDATE, UPDATEDATE, STATE ");
			sb.append("FROM INFOTYPE ");
			break;
		case 4:
			sb.append("SELECT INFOTYPEID, INFONAME, INFODESC, CREATEDATE, UPDATEDATE, STATE ");
			sb.append("FROM INFOTYPE ");
			break;
		case 5:
			sb.append("SELECT INFOTYPEID, INFONAME, INFODESC, CREATEDATE, UPDATEDATE, STATE ");
			sb.append("FROM INFOTYPE ");
			break;	
		case 6:
			sb.append("SELECT INFOTYPEID, INFONAME, INFODESC, CREATEDATE, UPDATEDATE, STATE ");
			sb.append("FROM INFOTYPE ");
			break;	
		default:
			break;
		}
		sb.append("LIMIT ");
		sb.append((pageno - 1) * pagesize);
		sb.append(", " + pagesize);
		String sql = sb.toString();
		return publicDao.getObjectList(sql);
	}

	public int saveInfoByType() {
		StringBuffer sb = new StringBuffer();
		sf.format(new Date());
		sb.append("INSERT INTO FOOD " );  
        sb.append("(FOOID, FOODNAME, CONTENT, STATE, PIC, NURSERY_ID, CREATEDATE, UPDATEDATE) "); 
        sb.append("VALUES (?, ?, ?)");
		return publicDao.update(sb.toString());
	}

	public int saveFood(String name, String desc, Integer state, Integer nid ,String fileName) {
		StringBuffer sb = new StringBuffer();
		String date = sf.format(new Date());
		sb.append("INSERT INTO FOOD " );  
        sb.append("(FOODNAME, CONTENT, STATE, PIC, NURSERY_ID, CREATEDATE, UPDATEDATE) "); 
        sb.append("VALUES ('"+name+"', '"+desc+"', '"+state+"', '"+fileName+"', '"+nid+"', '"+date+"',"+"'"+date+"')");
		return publicDao.update(sb.toString());
	}

	public int saveNotice(String name, String content, Integer state, Integer classid) {
		StringBuffer sb = new StringBuffer();
		String date = df.format(new Date());
		sb.append("INSERT INTO NOTICE " );  
        sb.append("(TITLE, CONTENT, CREATEDATE, UPDATEDATE, STATE, C_ID) "); 
        sb.append("VALUES ('"+name+"', '"+content+"', '"+date+"',"+"'"+date+"','"+state+"','"+classid+"')");
		return publicDao.update(sb.toString());
	}

	public List<Map<String, Object>> getFoodList(int pageno, int pagesize) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT FOODID, FOODNAME, CONTENT, STATE, PIC, NURSERY_ID, CREATEDATE, UPDATEDATE ");
		sb.append("FROM FOOD ");
		sb.append("LIMIT ");
		sb.append((pageno - 1) * pagesize);
		sb.append(", " + pagesize);
		String sql = sb.toString();
		logger.info(sql);
		return publicDao.getObjectList(sql);
	}

	public List<Map<String, Object>> getNoticeList(int pageno, int pagesize) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT NOTICEID, TITLE, CONTENT, CREATEDATE, UPDATEDATE,STATE,C_ID ");
		sb.append("FROM NOTICE ");
		sb.append("LIMIT ");
		sb.append((pageno - 1) * pagesize);
		sb.append(", " + pagesize);
		String sql = sb.toString();
		logger.info(sql);
		return publicDao.getObjectList(sql);
	}

	public int saveActive(Integer classid, String start, String end,
			String address, String content) {
		String startD = "";
		String endD = "";
		try {
			startD = df.format(sf.parse(start));
			endD = df.format(sf.parse(end));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String date = sf.format(new Date());
		StringBuffer sb = new StringBuffer();
		sf.format(new Date());
		sb.append("INSERT INTO ACTIVE " );  
        sb.append("(C_ID, ADDRESS, CONTENT, STARTDATE, ENDDATE, STATE, CREATEDATE, UPDATEDATE) "); 
        sb.append("VALUES ('"+classid+"', '"+address+"', '"+content+"', '"+startD+"',"+"'"+endD+"','"+1+"','"+date+"','"+date+"')");
		return publicDao.update(sb.toString());
	}

	public int saveParentingWork(Integer cid, String title, String content) {
		StringBuffer sb = new StringBuffer();
		String date = df.format(new Date());
		sb.append("INSERT INTO PARENTINGWORK " );  
        sb.append("(TITLE, CONTENT, CREATEDATE, UPDATEDATE, STATE, C_ID) "); 
        sb.append("VALUES ('"+title+"', '"+content+"', '"+date+"',"+"'"+date+"','"+1+"','"+cid+"')");
		return publicDao.update(sb.toString());
	}

	public int saveRemind(Integer nid, String content) {
		StringBuffer sb = new StringBuffer();
		String date = df.format(new Date());
		sb.append("INSERT INTO REMIND " );  
        sb.append("(CONTENT, CREATEDATE, UPDATEDATE, STATE, NURSERY_ID) "); 
        sb.append("VALUES ('"+content+"', '"+date+"',"+"'"+date+"','"+1+"','"+nid+"')");
		return publicDao.update(sb.toString());
	}

	public List<Map<String, Object>> getRemindList(int pageno, int pagesize) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT REMINDID, CONTENT, CREATEDATE, UPDATEDATE, STATE, NURSERY_ID ");
		sb.append("FROM REMIND ");
		sb.append("LIMIT ");
		sb.append((pageno - 1) * pagesize);
		sb.append(", " + pagesize);
		String sql = sb.toString();
		logger.info(sql);
		return publicDao.getObjectList(sql);
	}

	public List<Map<String, Object>> getParentingWorkList(int pageno, int pagesize) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT WORKID, TITLE, CONTENT, CREATEDATE, UPDATEDATE, STATE, C_ID ");
		sb.append("FROM PARENTINGWORK ");
		sb.append("LIMIT ");
		sb.append((pageno - 1) * pagesize);
		sb.append(", " + pagesize);
		String sql = sb.toString();
		logger.info(sql);
		return publicDao.getObjectList(sql);
	}

	public Map<String, Object> getRemindByID(int id) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT REMINDID, CONTENT, CREATEDATE, UPDATEDATE, STATE, NURSERY_ID ");
		sb.append("FROM REMIND ");
		sb.append("WHERE REMINDID = " + id);
		String sql = sb.toString();
		Map<String,Object> map = publicDao.getObjectMap(sql);
		logger.info(sql);
		return map;
	}

	public Map<String, Object> getParentingWorkByID(int id) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT WORKID, TITLE, CONTENT, CREATEDATE, UPDATEDATE, STATE, C_ID ");
		sb.append("FROM PARENTINGWORK ");
		sb.append("WHERE WORKID = " + id);
		String sql = sb.toString();
		Map<String,Object> map = publicDao.getObjectMap(sql);
		logger.info(sql);
		return map;
	}

	public int saveWeekCourse(Integer nid, Integer cid, Integer daysr,
			Integer weekNo, Integer courseid, String courseName) {
		StringBuffer sb = new StringBuffer();
		sb.append("INSERT INTO WEEKCOURSE " );  
        sb.append("(NURSERY_ID, C_ID, LESSON, WEEKNO, ISTHISWEEK, COURSENAME) "); 
        sb.append("VALUES ('"+nid+"', '"+cid+"', '"+daysr+"',"+"'"+weekNo+"','"+courseid+"','"+courseName+"')");
		return publicDao.update(sb.toString());
	}

	public List<Map<String, Object>> getWeekCourseList(int pageno, int pagesize, int cid) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT WC_ID, NURSERY_ID, C_ID, LESSON, WEEKNO, ISTHISWEEK, COURSENAME ");
		sb.append("FROM WEEKCOURSE WHERE C_ID = " + cid);
		sb.append(" LIMIT ");
		sb.append((pageno - 1) * pagesize);
		sb.append(", " + pagesize);
		String sql = sb.toString();
		logger.info(sql);
		return publicDao.getObjectList(sql);
	}

	public List<Map<String, Object>> getCourseList(int pageno, int pagesize,
			int cid) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT COURSEID, COURSENAME, T_ID, C_ID, NURSERY_ID ");
		sb.append("FROM COURSE WHERE C_ID = " + cid);
		sb.append(" LIMIT ");
		sb.append((pageno - 1) * pagesize);
		sb.append(", " + pagesize);
		String sql = sb.toString();
		logger.info(sql);
		return publicDao.getObjectList(sql);
	}

	public Map<String, Object> getFoodByID(int id) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT FOODID, FOODNAME, CONTENT, STATE, PIC, NURSERY_ID, CREATEDATE, UPDATEDATE ");
		sb.append("FROM FOOD WHERE FOODID = " + id);
		String sql = sb.toString();
		logger.info(sql);
		return publicDao.getObjectMap(sql);
	}

	public int deleteFoodByID(int id) {
		StringBuffer sb = new StringBuffer();
		sb.append("DELETE FROM FOOD WHERE FOODID = " + id);
		String sql = sb.toString();
		logger.info(sql);
		return publicDao.update(sql);
	}

	public int updateFood(Integer id, String name, String desc, Integer state,
			Integer nid, String newFileName) {
		StringBuffer sb = new StringBuffer();
		String date = sf.format(new Date());
		sb.append("UPDATE FOOD SET " );  
        sb.append("FOODNAME = '" + name +"', "); 
        sb.append("CONTENT = '" + desc +"', "); 
        sb.append("STATE = '" + state +"', "); 
        sb.append("PIC = '" + newFileName +"', "); 
        sb.append("NURSERY_ID = '" + nid +"', "); 
        sb.append("UPDATEDATE = '" + date +"' "); 
        sb.append("WHERE FOODID = "+id);
		return publicDao.update(sb.toString());
	}

	public int deleteNoticeByID(int id) {
		StringBuffer sb = new StringBuffer();
		sb.append("DELETE FROM NOTICE WHERE NOTICEID = " + id);
		String sql = sb.toString();
		logger.info(sql);
		return publicDao.update(sql);
	}

	public int updateNotice(Integer id, String title, String content,
			Integer state, Integer classid) {
		StringBuffer sb = new StringBuffer();
		String date = sf.format(new Date());
		sb.append("UPDATE NOTICE SET " );  
        sb.append("TITLE = '" + title +"', "); 
        sb.append("CONTENT = '" + content +"', "); 
        sb.append("STATE = '" + state +"', "); 
        sb.append("C_ID = '" + classid +"', "); 
        sb.append("UPDATEDATE = '" + date +"' "); 
        sb.append("WHERE NOTICEID = "+id);
		return publicDao.update(sb.toString());
	}

	public Map<String, Object> getNoticeByID(int id) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT NOTICEID, TITLE, CONTENT, CREATEDATE, UPDATEDATE,STATE,C_ID ");
		sb.append("FROM NOTICE WHERE NOTICEID = " + id);
		String sql = sb.toString();
		logger.info(sql);
		return publicDao.getObjectMap(sql);
	}

	public List<Map<String, Object>> getActiveList(int pageno, int pagesize) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ACTIVEID, C_ID, ADDRESS, CONTENT, STARTDATE, ENDDATE, STATE, CREATEDATE, UPDATEDATE ");
		sb.append("FROM ACTIVE ");
		sb.append(" LIMIT ");
		sb.append((pageno - 1) * pagesize);
		sb.append(", " + pagesize);
		String sql = sb.toString();
		logger.info(sql);
		return publicDao.getObjectList(sql);
	}

	public Map<String, Object> getActiveByID(int id) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ACTIVEID, C_ID, ADDRESS, CONTENT, STARTDATE, ENDDATE, STATE, CREATEDATE, UPDATEDATE FROM ACTIVE WHERE ACTIVEID = " + id);
		String sql = sb.toString();
		logger.info(sql);
		return publicDao.getObjectMap(sql);
	}

	public int deleteActiveByID(int id) {
		StringBuffer sb = new StringBuffer();
		sb.append("DELETE FROM ACTIVE WHERE ACTIVEID = " + id);
		String sql = sb.toString();
		logger.info(sql);
		return publicDao.update(sql);
	}

	public int updateActive(Integer id, Integer cid, String start, String end,
			String address, String content) {
		String startD = "";
		String endD = "";
		try {
			startD = df.format(sf.parse(start));
			endD = df.format(sf.parse(end));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String date = sf.format(new Date());
		StringBuffer sb = new StringBuffer();
		sf.format(new Date());
		
		sb.append("UPDATE ACTIVE SET " );  
        sb.append("C_ID = '" + cid +"', "); 
        sb.append("ADDRESS = '" + address +"', "); 
        sb.append("CONTENT = '" + content +"', "); 
        sb.append("STARTDATE = '" + startD +"', "); 
        sb.append("ENDDATE = '" + endD +"', "); 
        sb.append("STATE = '" + 1 +"', "); 
        sb.append("UPDATEDATE = '" + date +"' "); 
        sb.append("WHERE NOTICEID = "+id);
        
		return publicDao.update(sb.toString());
	}

	public int deleteRemindByID(int id) {
		StringBuffer sb = new StringBuffer();
		sb.append("DELETE FROM REMIND ");
		sb.append("WHERE REMINDID = " + id);
		String sql = sb.toString();
		logger.info(sql);
		return publicDao.update(sql);
	}

	public int updateRemind(Integer id, Integer nid, String content) {
		StringBuffer sb = new StringBuffer();
		String date = df.format(new Date());
		sb.append("UPDATE REMIND " );  
        sb.append(" SET CONTENT = '" + content +"', ");
        sb.append(" SET UPDATEDATE = '" + date +"', ");
        sb.append(" SET NURSERY_ID = '" + nid +"' ");
        sb.append("WHERE REMINDID = "+id);
        
		return publicDao.update(sb.toString());
	}

	public int deleteParentingWorkByID(int id) {
		StringBuffer sb = new StringBuffer();
		sb.append("DELETE ");
		sb.append("FROM PARENTINGWORK ");
		sb.append("WHERE WORKID = " + id);
		String sql = sb.toString();
		logger.info(sql);
		return publicDao.update(sql);
	}

	public int updateParentingWork(Integer id, Integer cid, String title,
			String content) {
		StringBuffer sb = new StringBuffer();
		String date = df.format(new Date());
		sb.append("UPDATE PARENTINGWORK " );  
        sb.append(" SET TITLE = '" + title +"', ");
        sb.append(" SET CONTENT = '" + content +"', ");
        sb.append(" SET UPDATEDATE = '" + date +"', ");
        sb.append(" SET C_ID = '" + cid +"' ");
        sb.append("WHERE REMINDID = "+id);
		return publicDao.update(sb.toString());
	}

	public int saveCourse(Integer nid, Integer cid, Integer isThisWeek,
			List<LinkedHashMap<String, Object>> list) {
		int re = 0;
		if(null != list && list.size() > 0){
			String[] sql = new String[list.size()];
			for(int i = 0;i < list.size() ; i++){
				Map<String, Object> map = list.get(i);
//		        Set<String> set = map.keySet();
		        StringBuffer sb = new StringBuffer();
		        sb.append("INSERT INTO WEEKCOURSE " );  
		        sb.append("(NURSERY_ID, C_ID, LESSON, WEEKNO, ISTHISWEEK, COURSENAME) "); 	
//		        for (Iterator<String> it = set.iterator();it.hasNext();) {
//		            String key = it.next();
//		            System.out.println(key + ":" + map.get(key));
		        sb.append("VALUES ('"+nid+"', '"+cid+"', '"+map.get("lesson")+"',"+"'"+map.get("week")+"','"+isThisWeek+"','"+map.get("name")+"')");
		        sql[i] = sb.toString();
//		        }
				
			}
			int[] i = publicDao.batchUpdate(sql);
			re = i.length;
		}
		return re;
	}

	public List<Map<String, Object>> getWeekCourselistByOtherID(Integer nid, Integer cid) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT WC_ID, NURSERY_ID, C_ID, LESSON, WEEKNO, ISTHISWEEK, COURSENAME ");
		sb.append("FROM WEEKCOURSE WHERE C_ID = " + cid);
		sb.append(" AND NURSERY_ID = " + cid);
		String sql = sb.toString();
		logger.info(sql);
		return publicDao.getObjectList(sql);
	}
	
}
